/**
 * @Description:租户配置页面
 * @Author: CHEHSHUANG
 * @Date: 2019/1/23
 */
import React, {Component} from 'react';
import {connect} from "react-redux"
import {Form, message, Tabs,Modal} from "antd"
import {
    findAdminByTenantCode,
    findRootByTenantCode,
    findTenantAppModule,
    listAllAppModule,
} from "./TenantService";
import {hide, show} from "../../../configs/SharedReducer";
import ConfigDepartmentRoot from "./ConfigDepartmentRoot";
import ConfigTenantAdmin from "./ConfigTenantAdmin";
import ConfigAppModule from "./ConfigAppModule";

const FormItem = Form.Item;
const TabPane = Tabs.TabPane;
const formLayout = {
    labelCol: {span: 6},
    wrapperCol: {span: 16}
}

class ConfigTenantModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            activeKey: "1",
            departmentRoot: null,
            allAppModule: null,
            tenantAppModule: null,
            tenantAdmin: null
        };
    }

    componentDidMount() {
        this.getDataSource();
    }
    refreshRoot = (departmentRoot) => {
        this.setState({departmentRoot})
    }
    getDataSource = () => {
        this.props.show();
        const tenantData = this.props.editData;
        const request = [listAllAppModule(),
            findRootByTenantCode(tenantData.code),
            findTenantAppModule(tenantData.id),
            findAdminByTenantCode(tenantData.code)
        ];
        Promise.all(request).then(res => {
            if (res[0]) {
                this.setState({allAppModule: res[0]})
            }
            if (res[1]) {
                this.setState({departmentRoot: res[1]})
            }
            if(res[2]){
                this.setState({tenantAppModule: res[2]});
            }
            if(res[3]){
                this.setState({tenantAdmin: res[3]});
            }
        }).catch(e => {
        }).finally(() => {
            this.props.hide();
        })
    }
    handleTabClick = (key) => {
        const {departmentRoot,tenantAdmin} = this.state;
        switch (key) {
            case "2":
                if(!departmentRoot){
                    message.error("请先配置部门根节点");
                    this.setState({activeKey: "1"});
                    return false;
                }
                break;
            default: break;
        }
        this.setState({activeKey: key});
    }

    render() {
        const {title, visible, onCancel, editData} = this.props;
        const {departmentRoot, allAppModule,tenantAppModule,tenantAdmin,activeKey} = this.state;
        return (
            <Modal
                width={800}
                centered
                destroyOnClose={true}
                maskClosable={false}
                title={title}
                visible={visible}
                onCancel={onCancel}
                footer={false}
            >
                <Tabs
                    onTabClick={(key)=> this.handleTabClick(key)}
                    activeKey={activeKey}
                >
                    <TabPane tab="配置部门根节点" key="1">
                        <ConfigDepartmentRoot
                            departmentRoot = {departmentRoot}
                            refresh={this.refreshRoot}
                            tenantCode={editData?editData.code: ""}
                        />
                    </TabPane>
                    <TabPane tab="配置租户管理员" key="2">
                        <ConfigTenantAdmin
                            tenantAdmin={tenantAdmin}
                            departmentRoot = {departmentRoot}
                            tenantCode={editData?editData.code: ""}
                        />
                    </TabPane>
                    <TabPane tab="配置应用模块" key="3">
                        <ConfigAppModule
                            editData={editData}
                            allAppModule = {allAppModule}
                            tenantAppModule={tenantAppModule}
                        />
                    </TabPane>
                </Tabs>
            </Modal>
        );
    }
}

const mapStateToProps = (state) => {
    return {};
};
const mapDispatchToProps = {hide, show};
export default connect(mapStateToProps, mapDispatchToProps)(ConfigTenantModal);
